
# underdog disparity in clustered lattice and random network

I <- 36
J <- 4
K <- I*J*6
N = 1000
L = 1000
T = 8
p = .19

mC = matrix(0,I*J,T)
MC = matrix(0,I*J,T)
mD = matrix(0,I*J,T)
MD = matrix(0,I*J,T)

SmC = matrix(0,N*L,T)
SMC = matrix(0,N*L,T)
SmD = matrix(0,N*L,T)
SMD = matrix(0,N*L,T)


# GENERATE NODES

lat <- matrix(1,I,J) 

for(i in 1:I)
{
	for(j in 1:J)
	{
		lat[i,j] = (i-1)*J+j
	}
}

# GENERATE EDGES

conn <- matrix(1,K,2)
count <- 1

for(i in 1:I)
{
	for(j in 1:J)
	{	
		conn[count,1] <- lat[i,j]
		conn[count,2] <- lat[i,(j - 2) %% J + 1]
		count <- count + 1

		conn[count,1] <- lat[i,j]
		conn[count,2] <- lat[i,j %% J + 1]
		count <- count + 1

		conn[count,1] <- lat[i,j]
		conn[count,2] <- lat[(i - 2) %% I + 1,j]
		count <- count + 1

		conn[count,1] <- lat[i,j]
		conn[count,2] <- lat[i %% I + 1,j]
		count <- count + 1

		if (j %% 2 == 0)
		{
			conn[count,1] <- lat[i,j]
			conn[count,2] <- lat[i %% I + 1,(j - 2) %% J + 1]
			count <- count + 1

			conn[count,1] <- lat[i,j]
			conn[count,2] <- lat[i %% I + 1, j %% J + 1]
			count <- count + 1
		}	

		if (j %% 2 == 1)
		{
			conn[count,1] <- lat[i,j]
			conn[count,2] <- lat[(i - 2) %% I + 1,(j - 2) %% J + 1]
			count <- count + 1

			conn[count,1] <- lat[i,j]
			conn[count,2] <- lat[(i - 2) %% I + 1,j %% J + 1]
			count <- count + 1
		}	
	}
}

connC <- conn

for(l in 1:L)
{
	# GENERATE EDGES - Diverse

	count <- 1
	check <- 0

	while(count <= K/2)
	{
		pick <- c(ceiling(runif(1)*K),ceiling(runif(1)*K))
	
		if(conn[pick[1],1] == conn[pick[2],2] | conn[pick[1],2] == conn[pick[2],1])
		{
			check <- 1
		}

		for(k in 1:K)
		{		
			if(conn[k,1] == conn[pick[1],1] & conn[k,2] == conn[pick[2],2])
			{
				check <- 1
			}

			if(conn[k,1] == conn[pick[2],1] & conn[k,2] == conn[pick[1],2])
			{
				check <- 1
			}
		}

		if (check == 0)
		{
			temp1 <- conn[pick[1],]
			temp2 <- conn[pick[2],]

			for(k in 1:K)
			{	
				if(conn[k,1] == temp1[1] & conn[k,2] == temp1[2])
				{
					conn[k,2] <- temp2[2]
				}

				if(conn[k,1] == temp2[1] & conn[k,2] == temp2[2])
				{
					conn[k,2] <- temp1[2]
				}

				if(conn[k,1] == temp1[2] & conn[k,2] == temp1[1])
				{
					conn[k,2] <- temp2[1]
				}

				if(conn[k,1] == temp2[2] & conn[k,2] == temp2[1])
				{
					conn[k,2] <- temp1[1]
				}
			}

			count <- count + 1
		}

		check <- 0
	}

	connD <- conn
	conn <- connC

	# GENERATE DIFFUSION PROCESSES

	for(n in 1:N)
	{

		mC[c(1),1] = 1
		MC[1 + c(1,2)*I*J/3,1] = 1
		mD[c(1),1] = 1
		MD[1 + c(1,2)*I*J/3,1] = 1

		for(t in 2:T)
		{
			a <- connC[which(connC[,1] %in% which(mC[,t-1] %in% 1)),2]
			mC[a,t] = (runif(length(a)) < p)

			a <- connC[which(connC[,1] %in% which(MC[,t-1] %in% 1)),2]
			MC[a,t] = (runif(length(a)) < p)

			a <- connD[which(connD[,1] %in% which(mD[,t-1] %in% 1)),2]
			mD[a,t] = (runif(length(a)) < p)

			a <- connD[which(connD[,1] %in% which(MD[,t-1] %in% 1)),2]
			MD[a,t] = (runif(length(a)) < p) 
		}

		for(t in 2:T)
		{
			mC[,t] = apply(mC[,1:t],1,max)
			MC[,t] = apply(MC[,1:t],1,max)
			mD[,t] = apply(mD[,1:t],1,max)
			MD[,t] = apply(MD[,1:t],1,max)
		}

		SmC[n+(l-1)*N,] = apply(mC,2,sum)
		SMC[n+(l-1)*N,] = apply(MC,2,sum)
		SmD[n+(l-1)*N,] = apply(mD,2,sum)
		SMD[n+(l-1)*N,] = apply(MD,2,sum)
	}

	mC = matrix(0,I*J,T)
	MC = matrix(0,I*J,T)
	mD = matrix(0,I*J,T)
	MD = matrix(0,I*J,T)
}


data <- cbind(c(1:T),apply(SmC,2,mean),t(apply(SmC,2,quantile,probs=c(.025,.975))),0,0)
data <- rbind(data,cbind(c(1:T),apply(SMC,2,mean),t(apply(SMC,2,quantile,probs=c(.025,.975))),0,1))
data <- rbind(data,cbind(c(1:T),apply(SmD,2,mean),t(apply(SmD,2,quantile,probs=c(.025,.975))),1,0))
data <- rbind(data,cbind(c(1:T),apply(SMD,2,mean),t(apply(SMD,2,quantile,probs=c(.025,.975))),1,1))

diff <- cbind(c(1:T),apply(SMC-SmC,2,mean),t(apply(SMC-SmC,2,quantile,probs=c(.025,.975))),0)
diff <- rbind(diff,cbind(c(1:T),apply(SMD-SmD,2,mean),t(apply(SMD-SmD,2,quantile,probs=c(.025,.975))),1))

write.table(data, file = "sim1.csv", sep = ",")
write.table(diff, file = "sim1_diff.csv", sep = ",")

q("no")

